package com.yeren.utils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;


/**
 * 数据库连接工具
 * @author liubiao
 *
 */
public class DBUtil {
	public static Connection getConn(String driverClass,String jdbcURL,String dba,String pwd){
		try {
			Class.forName(driverClass);
			return DriverManager.getConnection(jdbcURL,dba,pwd);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	public static Connection getConn(){
		Properties prop=PropertiesUtil.loadFile("dbconfig.properties");
		String jdbcUrl=prop.getProperty("jdbc_url");
		String jdbcDriverClass=prop.getProperty("jdbc_driver_class");
		String jdbcDba=prop.getProperty("jdbc_dba");
		String jdbcPwd=prop.getProperty("jdbc_pwd");
		Connection conn=DBUtil.getConn(jdbcDriverClass, jdbcUrl, jdbcDba, jdbcPwd);
		return conn;
	}
	
	public static void close(ResultSet rs,PreparedStatement pstmt,Connection conn){
		if (rs != null) {
			try {
				rs.close();
				rs = null;
			} catch (SQLException e) {
				
			}
		}
		if (pstmt != null) {
			try {
				pstmt.close();
				pstmt = null;
			} catch (SQLException e) {
			}
		}
		if (conn != null) {
			try {
				conn.close();
				conn = null;
			} catch (SQLException e) {
			}
		}
	}
}
